<template>
  <div class="role">
    <MBX l1="权限管理" l2="角色列表"></MBX>
    <el-card class="xia">
      <el-button type="primary" @click="addRole">添加角色</el-button>
      <el-table border class="xia" :data="formRole">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <div>
              <el-row
                class="q1"
                v-for="item1 in scope.row.children"
                :key="item1.id"
              >
                <!-- // 第一列 -->
                <el-col :span="6">
                  <el-tag
                    type="primary"
                    closable
                    @close="dell(scope.row.id, item1.id)"
                    >{{ item1.authName }}</el-tag
                  >
                </el-col>
                <!-- // 第二列 -->
                <el-col :span="18">
                  <el-row v-for="item2 in item1.children" :key="item2.id">
                    <el-col :span="6">
                      <el-tag
                        type="success"
                        closable
                        @close="dell(scope.row.id, item2.id)"
                        >{{ item2.authName }}</el-tag
                      >
                    </el-col>
                    <el-col :span="18">
                      <el-row v-for="item3 in item2.children" :key="item3.id">
                        <el-col :span="6">
                          <el-tag
                            type="warning"
                            closable
                            @close="dell(scope.row.id, item3.id)"
                            >{{ item3.authName }}</el-tag
                          >
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="TanEdit(scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="EditDel(scope.row.id)"
                >删除</el-button
              >
              <el-button
                type="warning"
                icon="el-icon-star-off"
                size="mini"
                @click="FenRole(scope.row)"
                >分配权限</el-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- // 添加模态框 -->
    <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
      <el-form :model="formInput">
        <el-form-item label="角色名称">
          <el-input v-model="formInput.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="formInput.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="GuanRole">确 定</el-button>
      </div>
    </el-dialog>
    <!-- // 编辑模态框 -->
    <el-dialog title="编辑角色" :visible.sync="dialogFormV">
      <el-form :model="formInputEdit">
        <el-form-item label="角色名称">
          <el-input v-model="formInputEdit.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="formInputEdit.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormV = false">取 消</el-button>
        <el-button type="primary" @click="GuanEditRole">确 定</el-button>
      </div>
    </el-dialog>
    <!-- // 模态框 -->
    <el-dialog title="提示" :visible.sync="dialogVisie" width="50%">
      <!-- // 树状图 -->
      <el-tree
        ref="TreeRef"
        :data="dataTree"
        show-checkbox
        node-key="id"
        default-expand-all
        :default-checked-keys="checkArr"
        :props="defaultProps"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="checkG">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { DelRoleapi } from '@/http/api'
export default {
  name: '',
  components: {},
  data() {
    return {
      // 选中
      checkArr: [],
      role: [],
      // 默认值
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      dataTree: [],
      formRole: [],
      RoleId: 0,
      // 添加
      formInput: {
        roleName: '',
        roleDesc: ''
      },
      // 编辑
      formInputEdit: {
        roleName: '',
        roleDesc: ''
      },
      EditId: 0,
      dialogFormVisible: false,
      dialogFormV: false,
      dialogVisie: false
    }
  },
  created() {
    this.getRole()
  },
  mounted() {},
  methods: {
    // 删除角色权限按钮
    async dell(roleId, rightId) {
      // console.log(row)
      console.log(roleId)
      console.log(rightId)
      const res = await this.$QHL.DellRoletApi(roleId, rightId)
      this.getRole()
      console.log(res)
      // row.children = res
    },

    async getRole() {
      const res = await this.$QHL.UserALLRolesapi()
      console.log(res)
      this.formRole = res
    },
    // 获取角色管理
    async Role() {
      const res = await this.$QHL.UserALLRolesapi()
      // console.log(res)
      this.role = res
      // console.log(res)
    },
    // 取消
    quxiao() {
      this.checkArr = []
      this.dialogVisie = false
    },
    // 分配角色
    async FenRole(row) {
      this.RoleId = row.id
      const res = await this.$QHL.Qxapi('tree')
      this.dataTree = res
      this.getCheckedArr(row, this.checkArr)
      this.dialogVisie = true
    },
    getCheckedArr(row, checkArr) {
      if (!row.children) {
        return checkArr.push(row.id)
      }
      row.children.forEach((item) => {
        this.getCheckedArr(item, checkArr)
      })
    },
    async checkG() {
      // 全选
      const allCheck = this.$refs.TreeRef.getCheckedKeys()
      // console.log(allCheck)
      const danCheck = this.$refs.TreeRef.getHalfCheckedKeys()
      // console.log(allCheck)
      // 将全选 半选合并
      const Arr = [...allCheck, ...danCheck]
      // 将合并的数组转为字符串
      const checkArr = Arr.join(',')
      // 调授权接口
      const res = await this.$QHL.ShouRoleapi(this.RoleId, checkArr)
      console.log(res)
      this.getRole()
      this.checkArr = []
      this.dialogVisie = false
    },
    // 添加弹框
    addRole() {
      this.formInput = {}
      this.dialogFormVisible = true
    },
    // 关闭，添加成功
    async GuanRole() {
      const res = await this.$QHL.AddRoleapi(this.formInput)
      console.log(res)
      this.getRole()
      this.dialogFormVisible = false
    },
    // 编辑弹框
    TanEdit(row) {
      console.log(row)
      // console.log(this.formInputEdit)
      this.formInputEdit.roleName = row.roleName
      this.formInputEdit.roleDesc = row.roleDesc
      this.EditId = row.id
      this.dialogFormV = true
    },
    // 关闭，编辑成功
    async GuanEditRole() {
      await this.$QHL.EditRoleapi(this.EditId, {
        from: this.formInputEdit
      })
      // console.log(res)
      this.getRole()
      this.dialogFormV = false
    },
    //删除
    EditDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DelRoleapi(id)
          .then((res) => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getRole()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      })
    }
  }
}
</script>
<style scoped lang="scss">
.el-dialog .el-input {
  width: 60%;
}
.q1 {
  padding: 30px 20px;
}
.el-tag {
  margin: 10px;
}
</style>
